package com.ktwlrj.core.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.ktwlrj.core.handler.MyMetaObjectHandler;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * MybatisPlusConfig配置
 *
 * @author zhouxuanhong
 * @date 2019-04-03 15:20
 * @since JDK1.8
 */
@Configuration
public class MybatisPlusConfig {
    /**
     * 分页插件，自动识别数据库类型
     *
     * @return PaginationInterceptor ${@link PaginationInterceptor}
     * @author zhouxuanhong
     * @date 2019-04-03 15:21
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }


    /**
     * sql日志打印(只用于开发和测试环境)
     *
     * @return MybatisConfiguration ${@link MybatisConfiguration}
     * @author zhouxuanhong
     * @date 2019-04-03 17:58
     */
    @Bean
    @Profile({"dev", "test"})
    public MybatisConfiguration mybatisConfiguration() {
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        // 日志打印(只在测试环境)
        mybatisConfiguration.setLogImpl(StdOutImpl.class);
        return mybatisConfiguration;
    }


    /**
     * sql公共字段自定义注入
     *
     * @return MyMetaObjectHandler ${@link MetaObjectHandler}
     * @author zhouxuanhong
     * @date 2019-04-03 18:10
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MyMetaObjectHandler();
    }


    /**
     * 数据库锁
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}
